unit FilterGui;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons,

  InventionCalc, CheckLst, ComCtrls, GlobalRecordsAndEnums;

type
  TfrmFilter = class(TForm)
    btnCancel: TButton;
    btnOk: TButton;
    btnAmarr: TSpeedButton;
    btnCaldari: TSpeedButton;
    btnGallente: TSpeedButton;
    btnMinmatar: TSpeedButton;
    gbRaces: TGroupBox;
    lblCategories: TLabel;
    lblGroups: TLabel;
    lblSubcategories: TLabel;
    lstCategories: TListView;
    lstSubcategories: TListView;
    lstGroups: TListView;
    lblCategorize: TLabel;
    lblSelectAll: TLabel;
    lblDeselectAll: TLabel;
    btnHelp: TButton;
    procedure btnOkClick(Sender: TObject);
    procedure lblSelectAllClick(Sender: TObject);
    procedure lblDeselectAllClick(Sender: TObject);
    procedure lstCategoriesClick(Sender: TObject);
    procedure lstGroupsClick(Sender: TObject);
    procedure lstSubcategoriesClick(Sender: TObject);
    procedure btnHelpClick(Sender: TObject);
    procedure lstCategoriesCustomDraw(Sender: TCustomListView;
      const ARect: TRect; var DefaultDraw: Boolean);
    procedure lstCategoriesCustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  private
    procedure PopulateLists;
  public
    categories, subcategories, groups: TList;

    constructor Create(AOwner: TComponent; invCalc: TInventionCalc; cats, subcats, grps: TList); reintroduce;
  end;

var
  frmFilter: TfrmFilter;
  inventionCalc: TInventionCalc;


implementation

{$R *.dfm}

uses
  Category, ExtActns;

constructor TfrmFilter.Create(Aowner: TComponent; invCalc: TInventionCalc; cats, subcats, grps: TList);
begin
  Inherited Create(AOwner);
  inventionCalc := invCalc;
  categories := cats;
  subcategories := subcats;
  groups := grps;

  btnAmarr.Down := inventionCalc.UserSettings.Settings.FilterAmarr;
  btnCaldari.Down := inventionCalc.UserSettings.Settings.FilterCaldari;
  btnGallente.Down := inventionCalc.UserSettings.Settings.FilterGallente;
  btnMinmatar.Down := inventionCalc.UserSettings.Settings.FilterMinmatar;

  lstCategories.DoubleBuffered := true;
  lstSubcategories.DoubleBuffered := true;
  lstGroups.DoubleBuffered := true;

  PopulateLists;
end;


procedure TfrmFilter.btnHelpClick(Sender: TObject);
var
  run: TFileRun;
begin
  run := TFileRun.Create(self);
  run.FileName := 'help\Filter.html';
  run.Execute;
  run.Free;
end;

procedure TfrmFilter.btnOkClick(Sender: TObject);
var
  i: Integer;
  item: TListItem;
begin
  categories.Clear;
  for i := 0 to lstCategories.Items.Count -1 do begin
    item := lstCategories.Items[i];
    if (item.Checked) then categories.Add(TCategory(item.Data));
  end;
  inventionCalc.UserSettings.Settings.FilterAmarr := btnAmarr.Down;
  inventionCalc.UserSettings.Settings.FilterCaldari := btnCaldari.Down;
  inventionCalc.UserSettings.Settings.FilterGallente := btnGallente.Down;
  inventionCalc.UserSettings.Settings.FilterMinmatar := btnMinmatar.Down;
end;


procedure TfrmFilter.lblSelectAllClick(Sender: TObject);
var
  i: Integer;
begin
  btnAmarr.Down := true;
  btnCaldari.Down := true;
  btnGallente.Down := true;
  btnMinmatar.Down := true;

  categories.Clear;
  for i := 0 to inventionCalc.CategoryCount -1 do begin
    categories.Add(inventionCalc.GetCategory(i));
  end;
  subcategories.Clear;
  for i := 0 to inventionCalc.SubcategoryCount -1 do begin
    subcategories.Add(inventionCalc.GetSubcategory(i));
  end;
  groups.Clear;
  for i := 0 to inventionCalc.GroupCount -1 do begin
    groups.Add(inventionCalc.GetGroup(i));
  end;
  PopulateLists;
end;

procedure TfrmFilter.lblDeselectAllClick(Sender: TObject);
begin
  btnAmarr.Down := false;
  btnCaldari.Down := false;
  btnGallente.Down := false;
  btnMinmatar.Down := false;
  categories.Clear;
  subcategories.Clear;
  groups.Clear;
  PopulateLists;
end;

procedure TfrmFilter.lstCategoriesClick(Sender: TObject);
var
  i: Integer;
  item: TListItem;
begin
  categories.Clear;
  for i := 0 to lstCategories.Items.Count -1 do begin
    item := lstCategories.Items[i];
    if (item.Checked) then categories.Add(TCategory(item.Data));
  end;
  PopulateLists;
end;

procedure TfrmFilter.lstCategoriesCustomDraw(Sender: TCustomListView;
  const ARect: TRect; var DefaultDraw: Boolean);
begin
//  lstCategories.Canvas.Brush.Color := clLtGray;
//  lstCategories.Canvas.FillRect(ARect);
end;

procedure TfrmFilter.lstCategoriesCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
//  lstCategories.Canvas.Brush.Color := clLtGray;
//  lstCategories.Canvas.Font.Color := clGray;
end;

procedure TfrmFilter.lstGroupsClick(Sender: TObject);
var
  i: Integer;
  item: TListItem;
begin
  groups.Clear;
  for i := 0 to lstGroups.Items.Count -1 do begin
    item := lstGroups.Items[i];
    if (item.Checked) then groups.Add(Tgroup(item.Data));
  end;
  PopulateLists;
end;

procedure TfrmFilter.lstSubcategoriesClick(Sender: TObject);
var
  i: Integer;
  item: TListItem;
begin
  subcategories.Clear;
  for i := 0 to lstSubcategories.Items.Count -1 do begin
    item := lstSubcategories.Items[i];
    if (item.Checked) then subcategories.Add(TSubcategory(item.Data));
  end;
  PopulateLists;
end;

procedure TfrmFilter.PopulateLists;
var
  i, j, k, l: Integer;
  category: TCategory;
  subcategory: TSubCategory;
  group: TGroup;
  item: TListItem;
begin
  lstCategories.Items.Clear;
  lstSubcategories.Items.Clear;
  lstGroups.Items.Clear;
  for i := 0 to inventionCalc.CategoryCount -1 do begin
    category := inventionCalc.GetCategory(i);
    item := lstCategories.Items.Add;
    item.Caption := category.Name;
    item.Data := category;
    for j := 0 to categories.Count -1 do begin
      if (TCategory(categories.Items[j]).ID = category.ID) then item.Checked := true;
    end;
    if (item.Checked) then begin
      for j := 0 to category.SubcategoryCount -1 do begin
        subcategory := category.GetSubcategory(j);
        item := lstSubcategories.Items.Add;
        item.Caption := subcategory.Category.Name + ' > ' + subcategory.Name;
        item.Data := subcategory;
        for k := 0 to subcategories.Count -1 do begin
          if (TSubcategory(subcategories.Items[k]).ID = subcategory.ID) then item.Checked := true;
        end;
        if (item.Checked) then begin
          for k := 0 to subcategory.GroupCount -1 do begin
            group := subcategory.GetGroup(k);
            item := lstGroups.Items.Add;
            item.Caption := group.Subcategory.Category.Name + ' > ' + group.Subcategory.Name + ' > ' + group.Name;
            item.Data := group;
            for l := 0 to groups.Count -1 do begin
              if (TGroup(groups.Items[l]).ID = group.ID) then item.Checked := true;
            end;
          end;
        end;
      end;
    end;
  end;
end;

end.
